<?php

        require_once "../../konfigurasi.php";
        
        koneksi_buka();
        
        $nilai=0;
        $uts_2=0;
        $remidi_1=0;
        $remidi_2=0;
        $remidi_3=0;
        
        
        if(isset($_POST['nilai'])){
                $nilai = $_POST['nilai'];
        }
        
        if(isset($_POST['uts_2'])){
                $uts_2 = $_POST['uts_2'];
        }
        
        if(isset($_POST['remidi_1'])){
                $remidi_1 = $_POST['remidi_1'];
        }
        if(isset($_POST['remidi_2'])){
                $remidi_2 = $_POST['remidi_2'];
        }
        if(isset($_POST['remidi_3'])){
                $remidi_3 = $_POST['remidi_3'];
        }
        
        $tahun = date("Y");          
        $tgl_daftar = date("Y-m-d");
        $wkt = date("H:i:s");
		
		$q_d_nilai_uts = mysql_query("select * from d_nilai_uts where nilai=$nilai");
		if($dt_d_nilai_uts= mysql_fetch_array($q_d_nilai_uts)){
			$d_nilai_uts = $dt_d_nilai_uts['id'];

		}else{
			$insert_d_nilai_uts = mysql_query("insert into d_nilai_uts (nilai) values($nilai)");
			$d_nilai_uts = mysql_insert_id();
		}	
		
		$q_d_d_nilai_uts = mysql_query("select * from d_d_nilai_uts where d_nilai_uts=$d_nilai_uts and nama='uts_2'");
		if($dt_d_d_nilai_uts= mysql_fetch_array($q_d_d_nilai_uts)){
			$d_d_nilai_uts = $dt_d_d_nilai_uts['id'];
			$update_d_d_nilai_uts = mysql_query("update d_d_nilai_uts set total=$uts_2 where id = $d_d_nilai_uts");
		}else{
			$insert_d_d_nilai_uts = mysql_query("insert into d_d_nilai_uts (nama, d_nilai_uts, total) values('uts_2', $d_nilai_uts , $uts_2)");
			$d_d_nilai_uts = mysql_insert_id();
		}	
		
				
		if($remidi_1>0){
			$result = mysql_query("delete from d_d_nilai_uts_remidi where nama='remidi_1' and d_d_nilai_uts=$d_d_nilai_uts");
			$q = "INSERT INTO d_d_nilai_uts_remidi
					(nama, total, d_d_nilai_uts) 
					VALUES('remidi_1', $remidi_1, $d_d_nilai_uts)";
		}
		if($remidi_2>0){
			$result = mysql_query("delete from d_d_nilai_uts_remidi where nama='remidi_2' and d_d_nilai_uts=$d_d_nilai_uts");
			$q = "INSERT INTO d_d_nilai_uts_remidi
					(nama, total, d_d_nilai_uts) 
					VALUES('remidi_2', $remidi_2, $d_d_nilai_uts)";
		}
		if($remidi_3>0){
			$result = mysql_query("delete from d_d_nilai_uts_remidi where nama='remidi_3' and d_d_nilai_uts=$d_d_nilai_uts");
			$q = "INSERT INTO d_d_nilai_uts_remidi
					(nama, total, d_d_nilai_uts) 
					VALUES('remidi_3', $remidi_3, $d_d_nilai_uts)";
		}
		

		$q_all_uts = mysql_query("select * from d_d_nilai_uts_remidi where d_d_nilai_uts=$d_d_nilai_uts");
		$i=0;
		while($dt_all_uts = mysql_fetch_array($q_all_uts)){
			$arr_all_uts[$i]=$dt_all_uts['total'];
			$i++;
		}
		$q_uts = mysql_query("select * from d_d_nilai_uts where id=$d_d_nilai_uts");
		if($dt_uts = mysql_fetch_array($q_uts)){
			$arr_all_uts[$i]=$dt_uts['total'];
			$i++;
		}
		$max_nilai=0;
		for($y=0;$y<$i;$y++){
			if($max_nilai<$arr_all_uts[$y]){
				$max_nilai=$arr_all_uts[$y];
			}
		}
		
		$update_d_d_nilai_uts = mysql_query("update d_d_nilai_uts set max=$max_nilai where id=d_d_nilai_uts");
		
		$q_nilai_uts = mysql_query("select * from d_d_nilai_uts where d_nilai_uts=$d_nilai_uts and nama='uts_1'");
		$uts_1 = 0;
		if($dt_nilai_uts=mysql_fetch_array($q_nilai_uts)){
			$uts_1 = $dt_nilai_uts['max'];
		}
		$rata = (($max_nilai+$uts_1)/2);
		$update_rata_d_nilai_uts = mysql_query("update d_nilai_uts set rata=$rata");
		
?>
